package org.tangerinejunction.imstats.ui.menu;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.tangerinejunction.imstats.ui.Application;
import org.tangerinejunction.imstats.ui.Preferences;

/**
 * Contains the main menu items.
 * 
 * @author meganthorsen
 */
public class MainMenu extends JMenuBar
{
	private static final long serialVersionUID = -2424230524346361693L;
	
	FileMenu fileMenu;
	ChartMenu chartMenu;
	PreferencesMenu preferencesMenu;
	final Application application;
	
	/**
	 * 
	 * @param application	the Application to which this MainMenu belongs
	 */
	public MainMenu(Application application)
	{
		this.application = application;
		fileMenu = new FileMenu();
		chartMenu = new ChartMenu();
		preferencesMenu = new PreferencesMenu();
		add(fileMenu);
		add(chartMenu);
		add(preferencesMenu);
	}
	
	/**
	 * Contains the File menu items.
	 * 
	 * @author meganthorsen
	 */
	public class FileMenu extends JMenu
	{
		private static final long serialVersionUID = 388822885810029357L;
		
		JMenuItem quitItem;
		
		public FileMenu()
		{
			super("File");
			quitItem = new JMenuItem(new AbstractAction("Quit")
			{
				private static final long serialVersionUID = 1670318344124024788L;

				public void actionPerformed(ActionEvent event) 
				{
					application.dispose();
					System.exit(0);
				}
			});
			add(quitItem);
		}
	}
	
	/**
	 * Contains the Chart menu items.
	 * 
	 * @author meganthorsen
	 */
	public class ChartMenu extends JMenu
	{
		private static final long serialVersionUID = 388822885810029357L;
		
		JMenuItem summaryItem;
		JMenuItem trendItem;
		JMenuItem cycleItem;
		JMenuItem statsItem;
		
		public ChartMenu()
		{
			super("Window");
			summaryItem = new JMenuItem(new AbstractAction("Summary")
			{
				private static final long serialVersionUID = 1670318344124024788L;

				public void actionPerformed(ActionEvent event) 
				{
					application.showSummaryChart();
				}
			});
			add(summaryItem);
			trendItem = new JMenuItem(new AbstractAction("Trend")
			{
				private static final long serialVersionUID = 1670318344124024788L;

				public void actionPerformed(ActionEvent event) 
				{
					application.showTrendChart();
				}
			});
			add(trendItem);
			cycleItem = new JMenuItem(new AbstractAction("Cycle")
			{
				private static final long serialVersionUID = 1670318344124024788L;

				public void actionPerformed(ActionEvent event) 
				{
					application.showCycleChart();
				}
			});
			add(cycleItem);
            statsItem = new JMenuItem(new AbstractAction("Stats")
            {
                private static final long serialVersionUID = 1670318344124024788L;

                public void actionPerformed(ActionEvent event) 
                {
                    application.showCorrespondentStatsChart();
                }
            });
            add(statsItem);
		}
	}
	
	public class PreferencesMenu extends JMenu 
	{
		JMenuItem labelOptionsItem;
		
		public PreferencesMenu()
		{
			super("Preferences");
			labelOptionsItem = new JMenuItem(new AbstractAction("Label Options")
			{
				public void actionPerformed(ActionEvent event) 
				{
					// TODO: show label options dialog
					LabelOptionsDialog dialog = new LabelOptionsDialog();
					dialog.setVisible(true);
				}
			});
			add(labelOptionsItem);
		}
	}
	
	private class LabelOptionsDialog extends JDialog implements ActionListener, ChangeListener
	{
		/**
		 * 
		 */
		private static final long serialVersionUID = -5930360410615807469L;
		JRadioButton name;
		JRadioButton shortName;
		JSpinner numChars;
		JRadioButton oneChar;
		JRadioButton ordinality;
		JCheckBox limitTooltip;
		
		public LabelOptionsDialog() 
		{
			super(MainMenu.this.application, "Label Options");
			createComponents();
			layoutComponents();
			setupEvents();
			pack();
			setLocationRelativeTo(null);
		}
		
		private void createComponents()
		{
			name = new JRadioButton("Entity/Account name");
			shortName = new JRadioButton("Entity/Account name abbreviated to ");
			oneChar = new JRadioButton("Single character of Entity/Account name");
			ordinality = new JRadioButton("Display order of entry into dataset");
			ButtonGroup group = new ButtonGroup();
			group.add(name);
			group.add(shortName);
			group.add(oneChar);
			group.add(ordinality);
			
			numChars = new JSpinner(new SpinnerNumberModel(8,1,30,1));
			
			limitTooltip = new JCheckBox("Also limit tooltip label");
			
			String labelOption = application.getPreferences().getLabelOption();
			int numCharsSelected = application.getPreferences().getNumLabelChars();
			boolean limitTooltipSelected = application.getPreferences().isLimitTooltipLabel();
			System.err.println("label option: " + labelOption + ", num chars: " + numCharsSelected + ", limit tooltip: " + limitTooltipSelected);
			if (labelOption.equals(Preferences.LABEL_FULL_NAME))
				name.setSelected(true);
			else if (labelOption.equals(Preferences.LABEL_SHORT_NAME))
				shortName.setSelected(true);
			else if (labelOption.equals(Preferences.LABEL_ONE_CHAR))
				oneChar.setSelected(true);
			else if (labelOption.equals(Preferences.LABEL_ORDINALITY))
				ordinality.setSelected(true);
			numChars.setValue(new Integer(numCharsSelected));
			limitTooltip.setSelected(limitTooltipSelected);
		}
		
		private void layoutComponents()
		{
			JPanel typePanel = new JPanel();
			typePanel.setBorder(BorderFactory.createTitledBorder("Label type"));
			JPanel miscPanel = new JPanel();
			miscPanel.setBorder(BorderFactory.createTitledBorder("Additional options"));
			
			GridBagLayout layout = new GridBagLayout();
			typePanel.setLayout(layout);
			GridBagConstraints constraints = layout.getConstraints(typePanel);		
			constraints.gridx = 0;
			constraints.gridy = 0;
			constraints.anchor = GridBagConstraints.NORTHWEST;
			
			constraints.gridwidth = 2;
			typePanel.add(name, constraints);
			constraints.gridy++;
			constraints.gridwidth = 1;
			typePanel.add(shortName, constraints);
			constraints.gridx = 1;
			typePanel.add(numChars, constraints);
			constraints.gridx = 0;
			constraints.gridwidth = 2;
			constraints.gridy++;
			typePanel.add(oneChar, constraints);
			constraints.gridy++;
			typePanel.add(ordinality, constraints);
			//constraints.gridy++;
			//add(Box.createVerticalStrut(10), constraints);
			//constraints.gridy++;
			miscPanel.add(limitTooltip, constraints);
			
			GridBagLayout layout2 = new GridBagLayout();
		    setLayout(layout2);
		    GridBagConstraints constraints2 = layout2.getConstraints(this);
		    constraints2.gridx = 0;
		    constraints2.gridy = 0;
		    constraints2.anchor = GridBagConstraints.NORTHWEST;
			add(typePanel, constraints2);
			constraints2.gridy++;
			add(miscPanel, constraints2);
		}
		
		private void setupEvents()
		{
			name.addActionListener(this);
			shortName.addActionListener(this);
			oneChar.addActionListener(this);
			ordinality.addActionListener(this);
			limitTooltip.addActionListener(this);
			numChars.addChangeListener(this);
		}

		public void actionPerformed(ActionEvent e) 
		{
			if (e.getSource() == name)
				application.getPreferences().setLabelOption(Preferences.LABEL_FULL_NAME);
			else if (e.getSource() == shortName)
				application.getPreferences().setLabelOption(Preferences.LABEL_SHORT_NAME);
			else if (e.getSource() == oneChar)
				application.getPreferences().setLabelOption(Preferences.LABEL_ONE_CHAR);
			else if (e.getSource() == ordinality)
				application.getPreferences().setLabelOption(Preferences.LABEL_ORDINALITY);
			else if (e.getSource() == limitTooltip)
				application.getPreferences().setLimitTooltipLabel(limitTooltip.isSelected());
			/* TODO: reload just the charts' labels or the charts if necessary */
		}

		public void stateChanged(ChangeEvent e) 
		{
			application.getPreferences().setNumLabelChars((Integer)numChars.getValue());
		}
	}
}
